package gui;


import javax.swing.*;

import util.*; 
import util.general.*;
import util.i3enum.MessageBox;
import util.i3enum.MessageBoxIcon;
import util.i3enum.config_path;

import dto.TotalFileBase;
import dto.TotalPartition;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author ThaiBaoDoan
 *
 */
public class TotalMainPanel  extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JMenuBar menubar;
	private JMenu mnFile , mnHelp, mnView;
	private JMenuItem imnPack, imnUnPack, imnExit, imnIndex, imnAbout,imnViewHidden, imnShowHidden;
	private List<JToggleButton> leftToolbar = new ArrayList<JToggleButton>();
	private List<JToggleButton> rightToolbar = new ArrayList<JToggleButton>(); 
	private config_path config = new config_path();
	MyList ml = new MyList(); //list 

    enum Positive {
    	LEFT,RIGHT,
    }
    enum EventType{
    	COMBOBOX,
    	BUTTON,
    }
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		
		if (event.getActionCommand().toString() == "Act_Left_Combobox") {
			ml.setCurrentFocus(ml.FOCUS_LEFT_TABLE);
			JComboBox source = (JComboBox) event.getSource();
			List<TotalFileBase> lfile = TotalFileBase.GetAllFile(source.getSelectedItem().toString()+":\\");
			if(lfile.size() > 0)
			{
			  DoChangePath(Positive.LEFT, source.getSelectedItem().toString(),
					EventType.COMBOBOX);
			}
			else
			{
				MessageBoxDialog.Show("Drive not found !", "Error", MessageBox.OK, MessageBoxIcon.ERROR); 
				ml.SetLeftCombobox(config.sLeftPath.substring(0,1));
			}
			return;
		}
		if (event.getActionCommand().toString() == "Act_Right_Combobox") {
			ml.setCurrentFocus(ml.FOCUS_RIGHT_TABLE);
			JComboBox source = (JComboBox) event.getSource(); 
			List<TotalFileBase> lfile = TotalFileBase.GetAllFile(source.getSelectedItem().toString()+":\\");
			if (lfile.size() >0 )
			{
			   DoChangePath(Positive.RIGHT, source.getSelectedItem().toString(),
					EventType.COMBOBOX);
			}
			else
			{
				MessageBoxDialog.Show("Drive not found !", "Error", MessageBox.OK, MessageBoxIcon.ERROR); 
				ml.SetRightCombobox(config.sRightPath.substring(0,1));
			}
			return;
		}
		if (event.getActionCommand().toString().contains("LEFT_")) {
			ml.setCurrentFocus(ml.FOCUS_LEFT_TABLE);
			JToggleButton source = (JToggleButton) event.getSource();
			List<TotalFileBase> lfile = TotalFileBase.GetAllFile(source.getText()+":\\");
		 
			if(lfile == null)
			{
				MessageBoxDialog.Show("Drive not found !", "Error", MessageBox.OK, MessageBoxIcon.ERROR); 
				SetLeftButtonBarOn("LEFT_"+config.sLeftPath.substring(0,1)); 
			    return ;
			}
			SetLeftButtonBarOn(source.getActionCommand());
			config.sLeftPath = source.getText() + ":\0";
			DoChangePath(Positive.LEFT, source.getText(), EventType.BUTTON);
			return;
		}
		if (event.getActionCommand().toString().contains("RIGHT_")) {
			ml.setCurrentFocus(ml.FOCUS_RIGHT_TABLE);
			JToggleButton source = (JToggleButton) event.getSource();
			List<TotalFileBase> lfile = TotalFileBase.GetAllFile(source.getText()+":\\");
			if(lfile == null)
			{
				MessageBoxDialog.Show("Drive not found !", "Error", MessageBox.OK, MessageBoxIcon.ERROR);
				SetRightButtonBarOn("RIGHT_"+config.sRightPath.substring(0,1)); 
			   return ;
			}
			SetRightButtonBarOn(source.getActionCommand());
			config.sRightPath = source.getText() + ":\0";
			DoChangePath(Positive.RIGHT, source.getText(), EventType.BUTTON);
			return;
		}
		//For buttons of below menu.
		if(event.getActionCommand().toString().startsWith(command_event.CM_PREFIX_BELOWBUTTON))
		{
			switch (event.getActionCommand().toString())
			{
			  case command_event.CM_BtnEXIT:
				  this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
				break;
			  case command_event.CM_BtnNEWFOLDER:
			  {
				  if(ml.getCurrentFocus() > 0)
				  {
				    NewFolderDialog dialog = new NewFolderDialog(this, true); 
				    String scurrentDir = "";
					if(ml.getCurrentFocus() == ml.FOCUS_LEFT_TABLE)
					{
						scurrentDir = ml.getLeftCurrentDir();
					}
					else if(ml.getCurrentFocus() == ml.FOCUS_RIGHT_TABLE)
					{
						scurrentDir = ml.getRightCurrentDir();
					}
					dialog.SetMyList(this.ml);
					dialog.SetParrentDirectory(scurrentDir);
				    dialog.setVisible(true);
				  }
			  }
			  break;
				default:
					return ;
					
			}
		}
	}
	public void SetLeftButtonBarOn(String Part)
	{
		for(int i = 0 ; i < leftToolbar.size() ; i++)
		{
			JToggleButton tbtn = (JToggleButton)leftToolbar.get(i);
			if(!tbtn.getActionCommand().equals(Part))
			{
				tbtn.setSelected(false);
			}
			else
			{ 
				tbtn.setSelected(true);
			} 
		}
		
	}
	public void SetRightButtonBarOn(String Part)
	{
		for(int i = 0 ; i < rightToolbar.size() ; i++)
		{
			JToggleButton tbtn = (JToggleButton)rightToolbar.get(i);
			if(!tbtn.getActionCommand().equals(Part))
			{
				tbtn.setSelected(false);
			}
			else
			{
				 
				tbtn.setSelected(true);
				 
			} 
		} 
		 
	}
   public JMenuBar CreateMenuBar()
	 {
		 this.menubar = new JMenuBar();
		 this.mnFile = new JMenu("File");
		 this.mnView = new JMenu("View");
		 this.mnHelp = new JMenu ("Help");
		 //File Menu 
		 this.imnPack = new JMenuItem("Pack");
		 this.mnFile.add(imnPack);
		 this.imnUnPack = new JMenuItem("Unpack");
		 this.mnFile.add(imnUnPack);
		 this.imnExit = new JMenuItem("Exit");
		 this.mnFile.add(imnExit);
		 //File View
		 this.imnShowHidden = new JMenuItem("Show hidden files");
		 this.mnView.add(imnShowHidden);
		 this.imnViewHidden = new JMenuItem("Don't show hidden files");
		 this.mnView.add(imnViewHidden);
		 //Help Menu
		 this.imnIndex = new JMenuItem("Index");
		 this.mnHelp.add(imnIndex);
		 this.imnAbout = new JMenuItem("About");
		 this.mnHelp.add(imnAbout);
		 
		 this.menubar.add(mnFile);
		 this.menubar.add(mnView);
		 this.menubar.add(mnHelp);
		 return this.menubar; 
	 }
	 public JPanel AboveButtonBar(TotalMainPanel root)
	 {
		JPanel btnBar =new JPanel(); 
	    JToolBar aboveLeft =new JToolBar();
	    JToolBar aboveRight =new JToolBar(); 
	    List<TotalPartition> _listTotalPart = TotalPartition.GetAllPartion();
	    List<TotalFileBase> _currentList;
		
	    for (int i = 0 ; i < _listTotalPart.size() ; i++)
	    {
	    	TotalPartition _part = _listTotalPart.get(i);
	    	ImageIcon _diskIcon = util.general.total_Image.ImageIconFromDiskType(_part.get_fileType()); 
	    	JToggleButton btnPartition1 = new JToggleButton(_part.get_stdPartionIndex(),_diskIcon); 
	    	JToggleButton btnPartition2 = new JToggleButton(_part.get_stdPartionIndex() ,_diskIcon);  
	    	btnPartition1.setPreferredSize(new Dimension(const_number.BUTTON_TOOLBAR_WIDTH,const_number.BUTTON_TOOLBAR_HEIGHT));
	    	btnPartition2.setPreferredSize(new Dimension(const_number.BUTTON_TOOLBAR_WIDTH,const_number.BUTTON_TOOLBAR_HEIGHT));
	    	btnPartition1.setActionCommand("LEFT_"+ _part.get_stdPartionIndex());
	    	btnPartition2.setActionCommand("RIGHT_"+ _part.get_stdPartionIndex());
	    	btnPartition1.addActionListener(root);
	    	btnPartition2.addActionListener(root);
	    	aboveLeft.add(btnPartition1);
	    	aboveRight.add(btnPartition2);
	    	leftToolbar.add(btnPartition1);
	    	rightToolbar.add(btnPartition2);
	    	if(config.sLeftPath.contains(_part.get_stdPartionIndex()))
	    	{
	    		 btnPartition1.setSelected(true); 
	    		 ml.SetLeftCombobox(_part.get_stdPartionIndex());
	    		 ml.SetDriveInformation(Positive.LEFT,TotalPartition.GetPartition(_part.get_stdPartionIndex()+":"));
	    		 _currentList = TotalFileBase.GetAllFile(_part.get_stdPartionIndex() +":\\");
	    		 if(_currentList.size() >0)
	    		 {
	    		     ml.Set_LeftCurrentList(_currentList,_part.get_stdPartionIndex()+":");
	    		 }
	    	}
	    	if(config.sRightPath.contains(_part.get_stdPartionIndex()))
	    	{
	    		btnPartition2.setSelected(true);
	    		ml.SetRightCombobox(_part.get_stdPartionIndex());
	    		 ml.SetDriveInformation(Positive.RIGHT,TotalPartition.GetPartition(_part.get_stdPartionIndex()+":"));
	    		 _currentList = TotalFileBase.GetAllFile(_part.get_stdPartionIndex() +":\\");
	    		 if(_currentList.size() > 0)
	    		 {
	    		    ml.Set_RightCurrentList(_currentList,_part.get_stdPartionIndex()+":");
	    		 }
	    	}
	    }
	    aboveLeft.setFloatable(false);
	    aboveRight.setFloatable(false);
	    aboveLeft.setRollover(true);
	    aboveRight.setRollover(true);
	    //Layout for left - right Panel
	    FlowLayout leftFollowLayout1 =new FlowLayout();
	    FlowLayout leftFollowLayout2 =new FlowLayout();
	    leftFollowLayout1.setAlignment(FlowLayout.LEFT);
	    leftFollowLayout2.setAlignment(FlowLayout.LEFT);
	    aboveLeft.setLayout(leftFollowLayout1);  
	    aboveRight.setLayout(leftFollowLayout2); 
	    btnBar.add(aboveLeft);
	    btnBar.add(aboveRight);
	    btnBar.setLayout(new BoxLayout(btnBar, BoxLayout.X_AXIS));
	    return btnBar;
	 }
	 public JPanel BelowButtonBar()
	 {
		 JPanel btnBar =new JPanel();
	    //Button View
	    JButton btnView = new JButton(const_string.NAME_BtnVIEW);
	    btnView.setPreferredSize(new Dimension(150,30));
	    btnView.setActionCommand(command_event.CM_BtnVIEW);
	    Icon iconView = new ImageIcon(total_Image.createImageIcon ("resources/images/view.png").getImage());
	    btnView.setIcon(iconView);
	    btnView.addActionListener(this);
	    btnBar.add(btnView);
	    //Button Edit
	    JButton btnEdit = new JButton(const_string.NAME_BtnEDIT);
	    btnEdit.setPreferredSize(new Dimension(150,30));
	    btnEdit.setActionCommand(command_event.CM_BtnEDIT);
	    Icon iconEdit = new ImageIcon(total_Image.createImageIcon ("resources/images/edit.png").getImage());
	    btnEdit.setIcon(iconEdit);
	    btnEdit.addActionListener(this);
	    btnBar.add(btnEdit);
	    //Button Copy
	    JButton btnCopy = new JButton(const_string.NAME_BtnCOPY);
	    btnCopy.setPreferredSize(new Dimension(150,30));
	    Icon iconCopy = new ImageIcon(total_Image.createImageIcon ("resources/images/copy.png").getImage());
	    btnCopy.setIcon(iconCopy);
	    btnCopy.setActionCommand(command_event.CM_BtnCOPY);
	    btnCopy.addActionListener(this);
	    btnBar.add(btnCopy);
	    
	    //Button Move
	    JButton btnMove = new JButton(const_string.NAME_BtnMOVE);
	    btnMove.setPreferredSize(new Dimension(150,30));
	    btnMove.setActionCommand(command_event.CM_BtnMOVE);
	    Icon iconMove = new ImageIcon(total_Image.createImageIcon ("resources/images/move.png").getImage());
	    btnMove.setIcon(iconMove);
	    btnMove.addActionListener(this);
	    btnBar.add(btnMove);
	    //Button New Folder
	    JButton btnNewFolder = new JButton(const_string.NAME_BtnNEWFOLDER);
	    btnNewFolder.setPreferredSize(new Dimension(150,30));
	    btnNewFolder.setActionCommand(command_event.CM_BtnNEWFOLDER);
	    Icon iconNewFolder = new ImageIcon(total_Image.createImageIcon ("resources/images/newfolder.png").getImage());
	    btnNewFolder.setIcon(iconNewFolder);
	    btnNewFolder.addActionListener(this);
	    btnBar.add(btnNewFolder);
	    
	    //Button Delete
	    JButton btnDelete = new JButton(const_string.NAME_BtnDELETE);
	    btnDelete.setPreferredSize(new Dimension(150,30));
	    btnDelete.setActionCommand(command_event.CM_BtnDELETE);
	    Icon IconDelete = new ImageIcon(total_Image.createImageIcon ("resources/images/delete.png").getImage());
	    btnDelete.setIcon(IconDelete);
	    btnDelete.addActionListener(this);
	    btnBar.add(btnDelete);
	    
	    //Button Exit
	    JButton btnExit = new JButton(const_string.NAME_BtnEXIT);
	    btnExit.setPreferredSize(new Dimension(150,30));
	    btnExit.setActionCommand(command_event.CM_BtnEXIT);
	    Icon IconExit = new ImageIcon(total_Image.createImageIcon ("resources/images//exit.png").getImage());
	    btnExit.setIcon(IconExit);
	    btnExit.addActionListener(this);
	    btnBar.add(btnExit);
	    //Buttons bar ...
	    btnBar.setLayout(new FlowLayout());
	    return btnBar;
	 }
	public void CreateAndShowGUI ()
	{
		Read_Set_Current_Dir();
		this.setTitle(const_string.SOFTWARE_NAME); 
		this.setJMenuBar(this.CreateMenuBar());
		JPanel newContentPane = new JPanel();
		newContentPane.setLayout(new BoxLayout(newContentPane,BoxLayout.Y_AXIS)); 
		//Items
		newContentPane.add(this.AboveButtonBar(this));
		ml.SetListenAction(this); //Set action for 2 comboboxs in list
		newContentPane.add(ml);
		newContentPane.add(this.BelowButtonBar());
		newContentPane.setPreferredSize(new Dimension(1158,630));
		
	    this.setContentPane(newContentPane);
	    this.setResizable(false);
	    this.setIconImage(util.general.total_Image.createImageIcon ("resources/images//total.png").getImage());
	    this.pack();
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true); 
		this.requestFocus();
		this.addWindowListener(new WindowAdapter() {
			 public void windowClosing(WindowEvent e) {
	                System.out.println("Save Current Directory");
	                config_general.Write_CurrentDir(config);
			 }
	 
		});
	}
	public void Read_Set_Current_Dir()
	{
		config = config_general.Read_CurrentDir();
		
	}
	public void DoChangePath(Positive Pos,String Part, EventType Type)
	{
		List<TotalFileBase> _currentList;
		_currentList = TotalFileBase.GetAllFile(Part +":\\");
		if(_currentList == null )
		{
			System.out.println("Can't access to director "+Part+"\n");
			return ;
		}
		if(Pos == Positive.LEFT)
		{
			//Get current Path for getting file, folder 
			//If can't get  return
			 
			if(Type == EventType.BUTTON)
			{
				ml.SetLeftCombobox(Part);
			}
			else if (Type == EventType.COMBOBOX)
			{
				SetLeftButtonBarOn("LEFT_"+Part);
			}
			ml.Set_LeftCurrentList(_currentList,Part+":");
			ml.SetDriveInformation(Positive.LEFT,TotalPartition.GetPartition(Part+":\\"));
		}
		else if (Pos == Positive.RIGHT)
		{
			if(Type == EventType.BUTTON)
			{
				ml.SetRightCombobox(Part);
			}
			else if (Type == EventType.COMBOBOX)
			{
				SetRightButtonBarOn("RIGHT_"+Part);
			}
			ml.Set_RightCurrentList(_currentList,Part+":");
			ml.SetDriveInformation(Positive.RIGHT,TotalPartition.GetPartition(Part+":\\"));
		}
	}
	 
}
